﻿/*
    Copyright 2012 Patricio Tapia Torres (patricio.tapia@gmail.com)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MarkupDesignForm
{
    public partial class consola : Form
    {
        private static consola ref_consola;
        private bool ver_taskbar = false;
        public static void escribir(string mensaje)
        {
            ref_consola.textBox1.Text += "\r\n" + mensaje;
            ref_consola.textBox1.SelectionStart = ref_consola.textBox1.TextLength;
            ref_consola.textBox1.ScrollToCaret();
        }
        public static bool IsValidHttpUri(string uriString)
        {
            Uri test = null;
            return Uri.TryCreate(uriString, UriKind.Absolute, out test) && test.Scheme == "http";
        }
        public consola(string[] parametros)
        {
            CheckForIllegalCrossThreadCalls = false; //Thread's Problems
            gethtml ghtml = new gethtml();
            InitializeComponent();
            if (parametros.Length > 0)
            {
                foreach (string u in parametros)
                {
                    //datosapasar += u;
                    //MessageBox.Show(u);
                    if (u == "console=true")
                    {
                        this.ShowInTaskbar = true;
                        this.WindowState = FormWindowState.Normal;
                        ver_taskbar = true;
                    }
                    if (u.Trim().Substring(0, 4) == "web=")
                    {
                        string[] websac = u.Split(new char[] { '=' });
                        textBox1.Text += "\n" + "Trying to access script:" + websac[1];
                        if (IsValidHttpUri(websac[1]))
                            ghtml.asyn_cargarpagina(websac[1], "ver=1", "POST", true, false);
                        else
                        {
                            MessageBox.Show("The Address is invalid", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            Application.Exit();
                        }
                    }
                }
            }
            if(!ver_taskbar)
                timer2.Enabled = true;
            ref_consola = this;
            //ghtml.asyn_cargarpagina("http://localhost/forms/add.xml", "ver=1", "POST", true,false);
            //ghtml.asyn_cargarpagina("http://localhost/login.xml", "ver=1", "POST", true, false);
        }

        private void consola_Load(object sender, EventArgs e)
        {

        }

        private void consola_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (textBox2.Text.Trim() == "list windows")
                {
                    consola.escribir("=========================");
                    consola.escribir("= Form's List   =");
                    consola.escribir("=========================");
                    foreach (Form f in Application.OpenForms)
                    {

                        consola.escribir(f.Name);
                    }
                    consola.escribir("==========================");
                }
                else if (textBox2.Text.Trim().Length > 4) 
                {
                    if (textBox2.Text.Trim().Substring(0, 5) == "close")
                    {
                        string[] p = textBox2.Text.Trim().Split(new char[] { ' ' });
                        if (p.Length == 2)
                        {
                            if (p[1].Length > 0)
                            {
                                foreach (Form foa in Application.OpenForms)
                                {
                                    if (foa.Name == p[1])
                                        foa.Close();
                                }
                            }
                        }
                    }
                    else if (textBox2.Text.Trim().Substring(0, 7) == "restart")
                    {
                        /*
                        string[] p = textBox2.Text.Trim().Split(new char[] { ' ' });
                        if (p.Length == 2)
                        {
                            
                        }
                         */
                    }
                }
                else if (textBox2.Text.Trim() == "restart")
                {
                    
                }
                else if (textBox2.Text.Trim() == "exit")
                    Application.Exit();
                textBox1.SelectionStart = textBox1.TextLength;
                textBox1.ScrollToCaret();
                textBox2.Text = "";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int totalv = 0;
            foreach (Form f in Application.OpenForms)
            {
                if (((f.Name != "consola") && (f.Name != "f_obteniendo") && (f.Name != "Form1")))
                    totalv++;
            }
            if (totalv == 0)
                Application.Exit();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            timer2.Enabled = false;
            this.Visible = false;
            this.Hide();
        }
    }
}
